import { Ast, Visitor, createRootAst } from "../ast";
/**
 * 发送评论功能
 */
export interface Send {
    send?: () => void;
}
export function clickSend(parent?: any) {
    const ctx: Send = {}
    const isRuning = () => {
        if (parent) return parent.getOtherCondiction()
        return true;
    }
    while (!ctx.send && isRuning()) {
        const root = createRootAst()
        const visitor = new SendVisitor()
        root.visit(visitor, ctx)
    }
    ctx.send()
}
export class SendVisitor implements Visitor {
    visitNode(node: Ast, ctx: any) {
        if (!node.visibleToUser) return;
        if (node.className === `android.widget.Button`) {
            return this.visitButton(node, ctx)
        }
        if (node.className === `android.widget.TextView`) {
            return this.visitTextView(node, ctx)
        }
        return node.children.map(child => child.visit(this, ctx))
    }
    visitButton(node: Ast, ctx: any) {
        if (node.id && node.id.endsWith(`live_asr_finish`)) {
            ctx.send = () => {
                node.click()
            }
        }
        if (node.text === `发送`) {
            ctx.send = () => {
                node.click()
            }
        }
    }
    visitTextView(node: Ast, ctx: any) {
        if (node.id && node.id.endsWith(`live_asr_finish`)) {
            ctx.send = () => {
                node.click()
            }
        }
        if (node.text === `发送`) {
            ctx.send = () => {
                node.click()
            }
        }
    }
}